package cn.loioi.multi.mysql.been;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import cn.loioi.multi.mysql.model.SysResource;
import tk.mybatis.mapper.common.Mapper;

public interface SysResourceMapper extends Mapper<SysResource> {
	@ResultType(String.class)
	@Select("SELECT DISTINCT CONCAT( res.identity, ':', rr.permission ) FROM sys_user_role AS role INNER JOIN sys_role_resource AS rr ON role.role = rr.role INNER JOIN sys_resource AS res ON rr.resource = res.id WHERE role.account = #{account}")
	List<String> permission(@Param("account") String account);

	@ResultMap("BaseResultMap")
	@Select("SELECT sys_resource.id, sys_resource.`name`, sys_resource.parentid, sys_resource.linkurl,sys_resource.icon, sys_resource.label, sys_resource.identity FROM sys_resource INNER JOIN sys_role_resource ON sys_role_resource.resource = sys_resource.id INNER JOIN sys_user_role ON sys_role_resource.role = sys_user_role.role WHERE sys_user_role.account = #{account} GROUP BY sys_resource.id ORDER BY sys_resource.parentid ASC, sys_resource.orderid ASC")
	List<SysResource> resources(@Param("account") String account);

	@ResultType(Map.class)
	@Select("SELECT sys_resource.id,sys_resource.`name`,sys_resource.parentid AS pId,sys_role_resource.role AS `checked` FROM sys_resource LEFT JOIN sys_role_resource ON sys_role_resource.resource = sys_resource.id AND sys_role_resource.role = #{roleID} ORDER BY sys_resource.parentid ASC, sys_resource.orderid ASC")
	List<Map<String, Object>> selectRoleResource(@Param("roleID") Integer roleID);

	@Delete("DELETE FROM `sys_role_resource` WHERE role=#{roleID}")
	int deleteRoleResource(@Param("roleID") Integer roleID);

	int insertRoleResource(@Param("roleID") Integer roleID, @Param("ids") Integer[] ids);

}